import {Context, Next} from 'koa';
import {IReturnInfo, ResponseBeautifier, ResponseInfo} from "../utils/ResponseBeautifier";
import _ from 'lodash';
import {GlobalVariable} from "../GlobalVariable";

export interface IRouterParame {
    path: string,
    handler: string,
    method: "get" | "post"
}

export class RouterCtrl {
    public static forbids = ["/system", "/public", "/static", "index"];

    public static checkProject(projectPath: string): IReturnInfo {
        if (!_.startsWith(projectPath, "/")) {
            return {code: 402, message: "项目的地址必须以“/”开头!"};
        }
        for (const forbid of RouterCtrl.forbids) {
            if (forbid === projectPath) {
                return {code: 404, message: "该路由禁止编辑!"};
            }
        }
        return {code: 200, message: "正常"};
    }

    public static checkRouter(parame: any): IReturnInfo {
        if (_.isEmpty(parame.handler)) {
            return {code: 401, message: "handler不能为空!"};
        }
        if (!_.startsWith(parame.path, "/")) {
            return {code: 403, message: "路径必须以“/”开头!"};
        }
        return {code: 200, message: "正常"};
    }


    public static addRouter(projectPath: string, parame: any) {
        if(parame.disable){
            return {code: 200,message: "成功!"};
        }
        const {path, handler, method} = parame;
        const router: any = GlobalVariable.router;
        const routrPath = projectPath + path;
        for (const layer of router.stack) {
            if (layer.path === routrPath) {
                return {code: 206, message: "路由已存在!"}
            }
        }
        return this.add(router, routrPath, method, handler);
    }

    public static add(router: any, path: string, method: string, handler: string) {
        try {
            router[method](path, new Function("ctx", "next", handler));
            return {code: 200, message: "添加成功!"};
        } catch (error: any) {
            console.log(error.message);
            return {code: 401, message: "添加失败!", data: error.message};
        }
    }

    public static removeRouter(projectPath: string, routerPath: any) {
        const path = projectPath + routerPath;
        const router = GlobalVariable.router;
        console.log(router.stack, path)
        _.remove(router.stack, (item) => {
            return item.path === path;
        });
        return {code: 200, message: "移除成功!"};
    }

    public static updateRouter(projectPath: string, oldPath: string, parame: any) {
        const result = this.removeRouter(projectPath, oldPath);
        if (result.code !== 200) {
            return result;
        }
        return this.addRouter(projectPath, parame);
    }

    public static removeByStartsWith(path: string) {
        const router = GlobalVariable.router;
        _.remove(router.stack, (item) => {
            return _.startsWith(item.path, path);
        });
        return {code: 200, message: "移除成功!"};
    }

    public static addByRouters(projectPath: string, routers: any[]) {
        routers.forEach((router: any) => {
            if (!router.disable) {
                const {path, method, handler} = router;
                router[method](projectPath + path, new Function("ctx", "next", handler));
            }
        })
    }


}
